<template>
	<view class="container">
		<view class="xiangmubox">
			<view @click="tosearchadress" class="rowitem">
				<view v-if="!adressInfo.addr" class="playshoder">选择您所在的社区地址</view>
				<view v-if="adressInfo.addr" class="adress">{{adressInfo.addr}}{{adressInfo.addrAoi}}</view>
				<uni-icons type="right" size="16" color="#333333"></uni-icons>
			</view>
			<view class="rowitem">
				<input class="quanbox" v-model="addrHouse" type="text" placeholder="填写具体地址（如10号楼1921）" />
			</view>
			<view class="rowitem">
				<input class="quanbox" v-model="mobile" type="number" maxlength="11" placeholder="填写您的电话号码" />
			</view>
			<view class="rowitem">
				<input class="quanbox" v-model="name" type="text" placeholder="填写你的姓名" />
			</view>
		</view>
		<view @click="gotoup" v-if="flag=='add'" class="baocunbtn">提交</view>
		<view @click="gotoup" v-if="flag=='set'" class="baocunbtn">保存</view>
	</view>
</template>
<script>
	import {
		addMemberAddress,
		editMemberAddress
	} from "../../request/api.js"
	import {
		requestPermissions
	} from "@/js_sdk/yu-app-permission/yu-app-permission.js"
	export default {
		data() {
			return {
				uid: '',
				flag: '', //add添加set编辑
				type: '', //1下单编辑  2地址管理编辑
				addrHouse: undefined,
				mobile: undefined,
				name: undefined,
				adressInfo: {
					addr: '河南省',
					addrAoi: '郑州市'
				},
				chooseOne: true,
				error: ''
			}
		},
		onLoad(options) {
			let that = this
			let adressinfo = JSON.parse(options.adressinfo)
			that.uid = getApp().globalData.uid
			that.flag = options.flag
			that.type = options.type
			that.adressInfo = adressinfo
			that.addrHouse = adressinfo.addrHouse
			that.name = adressinfo.name
			that.mobile = adressinfo.mobile
		},
		onShow() {
			let that = this
		},
		methods: {
			// 修改 地址
			gotoup() {
				let that = this
				if (!that.adressInfo.addrAoi) {
					uni.showToast({
						title: '请先选择地址',
						icon: 'none'
					});
					return
				}
				if (!(/^1[3456789]\d{9}$/.test(that.mobile))) {
					uni.showToast({
						title: '手机号有误，请重新填写！',
						icon: 'none'
					});
					return
				}
				if (!that.addrHouse) {
					uni.showToast({
						title: '请填写具体地址',
						icon: 'none'
					});
					return
				}
				if (!that.name) {
					uni.showToast({
						title: '请填写你的姓名',
						icon: 'none'
					});
					return
				}
				if (that.chooseOne) {
					uni.showLoading({
						title: '地址保存中',
					})
					that.chooseOne = false
				} else {
					return
				}
				let obj = JSON.parse(JSON.stringify(that.adressInfo))
				obj.addrHouse = that.addrHouse
				obj.name = that.name
				obj.mobile = that.mobile
				obj.memberId = that.uid
				obj.addrLabel = 0
				obj.addrType = 1
				obj.sex = 1;
				console.log(obj);
				if (that.flag == 'add') {
					addMemberAddress(obj).then(responts => {
						console.log(responts)
						if (responts.success) {
							setTimeout(function() {
								uni.hideLoading()
							})
							setTimeout(function() {
								uni.showToast({
									title: '保存成功',
									icon: 'none'
								})
							})
							// if (that.type == 1) {
							// 	var pages = getCurrentPages();
							// 	var prevPage = pages[pages.length - 2]; //获取上一个页面
							// 	// console.log('页面',prevPage)
							// 	// prevPage.$vm.adressinfo = that.adressInfo
							// }
							setTimeout(function() {
								uni.navigateBack(); //返回上一个页面
							}, 1000)
						} else {
							setTimeout(function() {
								uni.hideLoading()
							})
							setTimeout(function() {
								uni.showToast({
									title: '添加地址：' + responts.message,
									icon: 'none'
								})
								that.chooseOne = true
							})
						}
					}).catch(error => {
						setTimeout(function() {
							console.log(error)
							uni.hideLoading()
							that.chooseOne = true
						})
					})
				} else {
					editMemberAddress(obj).then(responts => {
						console.log(responts)
						if (responts.success) {
							setTimeout(function() {
								uni.hideLoading()
							})
							setTimeout(function() {
								uni.showToast({
									title: '保存成功',
									icon: 'none'
								})
							})
							if (that.type == 1) {
								var pages = getCurrentPages();
								var prevPage = pages[pages.length - 2]; //获取上一个页面
							}
							setTimeout(function() {
								uni.navigateBack(); //返回上一个页面
							}, 1000)
						} else {
							setTimeout(function() {
								uni.hideLoading()
							})
							setTimeout(function() {
								uni.showToast({
									title: responts.message,
									icon: 'none'
								})
								that.chooseOne = true
							})
						}
					}).catch(error => {
						setTimeout(function() {
							uni.hideLoading()
						})
						setTimeout(function() {
							that.chooseOne = true
						})
					})
				}
			},
			// #ifdef APP
			// 跳转到搜索地址页面
			async tosearchadress() {
				let that = this;
				// 获取用户当前位置
				console.log('aa');
				const {
					isSuc
				} = await requestPermissions({
					title: "位置权限申请说明", // 申请权限时弹出框标题
					content: "便于您使用该功能选择位置场景中使用", // 申请权限时弹出框内容
					permissionID: "ACCESS_FINE_LOCATION" // 权限ID
				})
				if (!isSuc) {
					return false
				}
				uni.getLocation({
					success: (res) => {
						const defaultLocation = {
							latitude: res.latitude,
							longitude: res.longitude
						};
						console.log(defaultLocation);
						// 设置默认地点并调用选择位置
						uni.chooseLocation({
							latitude: defaultLocation.latitude,
							longitude: defaultLocation.longitude,
							success: (item) => {
								that.error = JSON.stringify(item)
								console.log(item);
								that.adressInfo.addr = item.address;
								that.adressInfo.addrAoi = item.name;
								that.adressInfo.lng = item.longitude;
								that.adressInfo.lat = item.latitude;
								that.$forceUpdate();
							}
						});
					}
				});
			}
			// #endif
			// #ifdef H5
			tosearchadress() {
				let that = this;
				// 获取用户当前位置
				console.log('bb');
				uni.chooseLocation({
					success: (item) => {
						console.log(item);
						that.adressInfo.addr = item.address;
						that.adressInfo.addrAoi = item.name;
						that.adressInfo.lng = item.longitude;
						that.adressInfo.lat = item.latitude;
						that.$forceUpdate();
					}
				});
			}
			// #endif
			// wx.navigateTo({
			// 	url: '../searchbadress/index?adressinfo=' + JSON.stringify(this.adressInfo),
			// })
		}
	}
</script>
<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100%;
		background-color: #EEEEEE;
		box-sizing: border-box;

		.xiangmubox {
			width: 100%;
			box-sizing: border-box;
			padding: 0 28rpx;
			margin-bottom: 50rpx;
			background-color: #fff;
			font-size: 28rpx;
			color: #333333;

			.rowitem {
				width: 100%;
				height: 104rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 2rpx solid #eee;

				.adress {
					width: 634rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					-webkit-box-orient: vertical;
				}

				.quanbox {
					width: 100%;
					height: 64rpx;
					font-size: 28rpx;
					color: #333333;
				}

				.playshoder {
					color: #999999;
				}

				&>image {
					width: 30rpx;
					height: 30rpx;
					padding: 6rpx;
				}
			}
		}

		.baocunbtn {
			width: 694rpx;
			height: 84rpx;
			line-height: 84rpx;
			text-align: center;
			background-color: #333333;
			font-size: 32rpx;
			color: #fff;
			border-radius: 8rpx;
			margin: 22rpx auto;
		}
	}
</style>